﻿Ext.define('app.system.user.Index',
{
    extend: 'Ext.grid.Panel',
    border: true,
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    //multiColumnSort: true,
    forceFit: false,
    autoScroll: true,
    initComponent: function () {
        var me = this;
        me.store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/user/GetAll', filter: {}, remoteSort: false });

        var txt_account = new Ext.form.field.Text({ emptyText: '账号' });
        var txt_name = new Ext.form.field.Text({ emptyText: '姓名' });
        me.tbar = [
            txt_account,
            "&nbsp;-&nbsp;",
            txt_name,
            {
                xtype: 'button', text: '查询', handler: function () {
                    var account = txt_account.getValue();
                    var name = txt_name.getValue();
                    me.store.clearFilter();
                    if (account && account != "") {
                        me.store.filterBy(function (record) { return record.data.userName.indexOf(account) != -1; });
                    }
                    if (name && name != "") {
                        me.store.filterBy(function (record) { return record.data.name.indexOf(name) != -1; });
                    }
                }
            },
            {
                xtype: 'button', icon: '/Content/images/add.png', text: '新增', handler: function () {
                    var win = Ext.create("app.system.user.Create", { title: '新增用户' });
                    win.on("savesuccess", function () {
                        me.store.load();
                    });
                    win.show();
                }
            },
            {
                xtype: 'button', icon: '/Content/images/add.png', text: '导入', handler: function () {
                    var win = Ext.create("app.system.user.Import", { title: '导入用户' });
                    win.on("savesuccess", function () {
                        me.store.load();
                    });
                    win.show();
                }
            },
            "->",
             {
                 text: localize('Refresh'), glyph: 0xf021, handler: function () {
                     me.store.load();
                 }
             }
        ];
        me.callParent(arguments);
    },
    columns: [
        {
            xtype: 'actioncolumn',
            width: 70,
            header: localize('Operating'),
            items: [
            {
                icon: '/Content/images/table_link.png',
                tooltip: '用户授权',
                handler: function (grid, rowIndex, colIndex) {
                    var record = grid.store.getAt(rowIndex);
                    var win = Ext.create("app.system.user.SetPermission", { title: '用户授权', userId: record.data.id });
                    win.on("savesuccess", function () {
                        win.close();
                    });
                    win.show();
                }
            },
            {
                icon: '/Content/images/lock.png',
                tooltip: '重置密码',
                handler: function (grid, rowIndex, colIndex) {
                    var record = grid.store.getAt(rowIndex);
                    Ext.Msg.confirm('系统提示', '确定要初始化密码吗？', function (btn) {
                        if (btn == 'yes') {
                            App.AjaxPost('/api/services/app/user/InitPass?id=' + record.data.id, {}, function (data) {
                                Ext.Msg.alert("系统提示", "密码重置成功");
                            });
                        }
                    });
                }
            },
            {
                icon: '/Content/images/edit.gif',
                tooltip: '编辑',
                handler: function (grid, rowIndex, colIndex) {
                    var record = grid.store.getAt(rowIndex);
                    var win = Ext.create("app.system.user.Edit", { title: '用户编辑', userId: record.data.id });
                    win.on("savesuccess", function () {
                        grid.store.load();
                        win.close();
                    });
                    win.show();
                }
            }
            ]
        },
        { header: '账号', dataIndex: 'userName' },
        { header: '姓名', dataIndex: 'name' },
        { header: '事业部', dataIndex: 'businessName' },
        { header: '研发中心', dataIndex: 'rdCenterName' },
        { header: '研究室', dataIndex: 'laboratoryName' },
        {
            header: '是否可用', dataIndex: 'isActive', renderer: function (value, p, record) {
                return value ? "可用" : "<a style='color:red'>不可用</a>";
            }
        },
        {
            header: '角色', dataIndex: 'roles', renderer: function (value, p, record) {
                var roleNames = '';
                for (var j = 0; j < value.length; j++) {
                    if (roleNames.length) {
                        roleNames = roleNames + ', ';
                    }

                    roleNames = roleNames + value[j].roleName;
                };

                return roleNames;
            }
        },
        {
            header: '区域', dataIndex: 'quan'
        },
        {
            header: '最后登录时间', dataIndex: 'lastLoginTime', renderer: function (value, p, record) {
                return Ext.util.Format.date(value, 'Y-m-d')
            }
        },
        {
            header: '创建时间', dataIndex: 'creationTime', renderer: function (value, p, record) {
                return Ext.util.Format.date(value, 'Y-m-d')
            }
        }
    ]
});
